import 'dart:async';

import 'package:amap_location_fluttify/amap_location_fluttify.dart';
import 'package:auto_updater/auto_updater.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:palmf_hid/common/services/services.dart';
import 'package:palmf_hid/common/store/store.dart';
import 'package:palmf_hid/common/utils/storage_manager.dart';
import 'package:palmf_hid/common/utils/utils.dart';
import 'package:get/get.dart';
import 'package:wakelock_plus/wakelock_plus.dart';

import 'common/services/Db.dart';
import 'common/store/user.dart';
import 'common/utils/WindowSizeAdapter.dart';
import 'common/utils/net/http_util.dart';
import 'common/values/values.dart';

/// 全局静态数据
class Global {
  /// 初始化
  static Future init() async {
    WidgetsFlutterBinding.ensureInitialized();
    //滚动性能优化 1.22.0
    GestureBinding.instance.resamplingEnabled = true;
    WindowSizeAdapter.setSize();
    //保持屏幕常量
    WakelockPlus.enable();
    AmapLocation.instance.updatePrivacyShow(true);

    ///todo 必须保证在调用定位功能之前调用, 建议首次启动App时弹出《隐私政策》并取得用户同意
    ///todo [hasAgree] 隐私权政策是否已经取得用户同意
    AmapLocation.instance.updatePrivacyAgree(true);

    /// !注意: 只要是返回Future的方法, 一律使用`await`修饰, 确保当前方法执行完成后再执行下一行, 在不能使用`await`修饰的环境下, 在`then`方法中执行下一步.
    /// 初始化 iOS在init方法中设置, android需要去AndroidManifest.xml里去设置, 详见 https://lbs.amap.com/api/android-sdk/gettingstarted
    await AmapLocation.instance
        .init(iosKey: '4757ad92a3ee4c6d945c0f392f0fec40');

    await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
    setSystemUi();
    Loading();

    if (!GetPlatform.isWeb) {
      await StorageManager.init();
    }else if(GetPlatform.isWindows){
      MyLogUtil().i("-------------");
      String feedURL = 'http://192.168.51.206:5421/api/download?filename=appcast.xml&token=7ce6637e035e887ae6979998024769d6&timestamp=1731034846051';
      await autoUpdater.setFeedURL(feedURL);
      await autoUpdater.checkForUpdates();
      await autoUpdater.setScheduledCheckInterval(3600);

    }
    await Get.putAsync<StorageService>(() => StorageService().init());
    Get.putAsync<DbService>(() => DbService().init());
    Get.put<ConfigStore>(ConfigStore());
    Get.put<UserStore>(UserStore());
    HttpUtils.init(
      baseUrl: ApiConstants.hiproUrl,
    );
  }

  static void setSystemUi() {
    if (GetPlatform.isAndroid) {
      SystemUiOverlayStyle systemUiOverlayStyle = const SystemUiOverlayStyle(
        statusBarColor: Colors.transparent,
        statusBarBrightness: Brightness.light,
        statusBarIconBrightness: Brightness.dark,
        systemNavigationBarDividerColor: Colors.transparent,
        systemNavigationBarColor: Colors.white,
        systemNavigationBarIconBrightness: Brightness.dark,
      );
      SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
    }
  }
}
